package net.lahuobao.mall.controller.admin;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.lahuobao.mall.entity.admin.MallSubsidyEntity;
import net.lahuobao.mall.entity.admin.querymodel.MallManageProductQueryEntity;
import net.lahuobao.mall.entity.admin.resultmodel.MallManageProductResultEntity;
import net.lahuobao.mall.service.admin.MallSubsidyServiceI;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import net.lahuobao.mall.entity.admin.MallProductEntity;
import net.lahuobao.mall.service.admin.MallProductServiceI;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.jeecgframework.core.beanvalidator.BeanValidators;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.net.URI;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;

/**   
 * @Title: Controller
 * @Description: 商品
 * @author zhangdaihao
 * @date 2016-09-12 15:02:08
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/mallProductController")
public class MallProductController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MallProductController.class);

	@Autowired
	private MallProductServiceI mallProductService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;

	@Autowired
	private MallSubsidyServiceI subsidyService;
	

	/*################### 每日推荐 start ##################*/
	/**
	 * 商品列表 页面跳转--每日推荐
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("net/lahuobao/mall/admin/mallProductList");
	}

	/**
	 * easyui AJAX请求数据--每日推荐
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(MallProductEntity mallProduct,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(MallProductEntity.class, dataGrid);
		//查询条件组装器
		if(StringUtil.isNotEmpty(mallProduct.getKeywords())){
			cq.like("keywords", "%"+mallProduct.getKeywords()+"%");
			mallProduct.setKeywords(null);
		}

		cq.eq("status", 1);

		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mallProduct, request.getParameterMap());
		this.mallProductService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除商品
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(MallProductEntity mallProduct, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		mallProduct = systemService.getEntity(MallProductEntity.class, mallProduct.getId());
		message = "商品删除成功";
		mallProductService.delete(mallProduct);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}
	/*################### 每日推荐 end ##################*/


	/*################### 商品管理 start ##################*/
	/**
	 * 商品列表 页面跳转--商品管理
	 *
	 * @return
	 */
	@RequestMapping(params = "managelist")
	public ModelAndView manageList(HttpServletRequest request) {
		return new ModelAndView("net/lahuobao/mall/admin/mallProductManageList");
	}

	/**
	 * easyui AJAX请求数据--商品管理
	 *
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param
	 */

	@RequestMapping(params = "managedatagrid")
	public void manageDatagrid(MallManageProductQueryEntity query,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
	/*	CriteriaQuery cq = new CriteriaQuery(MallProductEntity.class, dataGrid);
		//查询条件组装器
		if(StringUtil.isNotEmpty(mallProduct.getProductname())){
			cq.like("productname", "%"+mallProduct.getProductname()+"%");
			mallProduct.setProductname(null);
		}

		if(null !=mallProduct.getMallShopEntity()  && StringUtil.isNotEmpty(mallProduct.getMallShopEntity().getShopname())){
			cq.like("mallShopEntity.shopname", "%"+mallProduct.getMallShopEntity().getShopname()+"%");
			mallProduct.setMallShopEntity(null);
		}
		//cq.eq("status", 1);

		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mallProduct, request.getParameterMap());
		this.mallProductService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);*/
		List<MallManageProductResultEntity> list = mallProductService.selectProduct(query);
		TagUtil.datagrid(response, pageDateGrid(list, dataGrid));
	}

	/**
	 * 更新商品信息
	 *
	 * @return
	 */
	@RequestMapping(params = "update")
	@ResponseBody
	public AjaxJson save(MallProductEntity mallProduct, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(mallProduct.getId())) {
			message = "商品更新成功";
			MallProductEntity t = mallProductService.get(MallProductEntity.class, mallProduct.getId());
			try {
				//通过启用禁用上架下架商品
				if(StringUtil.isNotEmpty(mallProduct.getIsdelete().toString()) && "1".equals(mallProduct.getIsdelete().toString())){//禁用
					mallProduct.setStatus(2);
				}
				if(StringUtil.isNotEmpty(mallProduct.getIsdelete().toString()) && "0".equals(mallProduct.getIsdelete().toString())){//禁用
					mallProduct.setStatus(1);
				}
				MyBeanUtils.copyBeanNotNull2Bean(mallProduct, t);
				mallProductService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "商品管理更新失败";
			}
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 获取商品折扣编辑页面
	 * @param productId
	 * @return
	 */
	@RequestMapping(params = "subsidyview")
	public ModelAndView getSubsidyView(String productId, String look){
		boolean isLook = false;
		ModelAndView mv = new ModelAndView("net/lahuobao/mall/admin/mallProductSubsidy");
		MallProductEntity product  = mallProductService.getEntity(MallProductEntity.class, Integer.valueOf(productId));
		mv.addObject("mallProductPage", product);
		//获取折扣信息
		MallSubsidyEntity subsidy = subsidyService.selectSubsidyByProductId(product.getId());
		mv.addObject("subsidy", subsidy);
		if(StringUtil.isNotEmpty(look) && look.equals("true")){
			isLook = true;
		}
		mv.addObject("islook", isLook);
		return mv;
	}



	/*################### 商品管理 end ##################*/

	

}
